package com.yyl.common.util;

import com.yyl.common.system.vo.SysPermissionDataRuleModel;
import com.yyl.common.system.vo.SysUserCacheInfo;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

// 数据权限查询规则容器工具类
public class JeecgDataAutorUtils {
	public static final String MENU_DATA_AUTHOR_RULES = "MENU_DATA_AUTHOR_RULES";
	
	public static final String MENU_DATA_AUTHOR_RULE_SQL = "MENU_DATA_AUTHOR_RULE_SQL";
	
	public static final String SYS_USER_INFO = "SYS_USER_INFO";
	
	/**
	 * 往链接请求里面，传入数据查询条件
	 * @param request
	 * @param dataRules
	 */
	public static synchronized void installDataSearchConditon(HttpServletRequest request, List<SysPermissionDataRuleModel> dataRules){
		// 1.先从request获取MENU_DATA_AUTHOR_RULES，如果存则获取到LIST
		List<SysPermissionDataRuleModel> list=(List<SysPermissionDataRuleModel>) loadDataSearchConditon();
		if (list==null){
			//2.如果不存在，则new一个list
			list=new ArrayList<SysPermissionDataRuleModel>();
		}
		list.addAll(dataRules);
		request.setAttribute(MENU_DATA_AUTHOR_RULES,list);// 往 list 里面增量存指
	}
	// 获取请求对应的数据权限规则
	@SuppressWarnings("unchecked")
	public static synchronized List<SysPermissionDataRuleModel> loadDataSearchConditon(){
		return (List<SysPermissionDataRuleModel>) SpringContextUtils.getHttpServletRequest().getAttribute(MENU_DATA_AUTHOR_RULES);
	}
	//获取请求对应的数据权限SQL
	public static synchronized String loadDataSearchConditonSQLString(){
		return (String) SpringContextUtils.getHttpServletRequest().getAttribute(MENU_DATA_AUTHOR_RULE_SQL);
	}
	// 往链接请求里面，传入数据查询条件
	public static synchronized void installDataSearchCondition(HttpServletRequest request,String sql){
		String ruleSql=(String) loadDataSearchConditonSQLString();
		if (!StringUtils.hasText(ruleSql)){
		  request.setAttribute(MENU_DATA_AUTHOR_RULE_SQL,sql);
		}
	}
	public static synchronized void installUserInfo(HttpServletRequest request, SysUserCacheInfo userinfo) {
		request.setAttribute(SYS_USER_INFO, userinfo);
	}
	
	public static synchronized SysUserCacheInfo loadUserInfo() {
		return (SysUserCacheInfo) SpringContextUtils.getHttpServletRequest().getAttribute(SYS_USER_INFO);
		
	}
}
